<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>虚拟代理</title>
</head>
<body>
    <div id="div1">
        <input type="checkbox" id="1"/>1
        <input type="checkbox" id="2"/>2
        <input type="checkbox" id="3"/>3
        <input type="checkbox" id="4"/>4
        <input type="checkbox" id="5"/>5
        <input type="checkbox" id="6"/>6
        <input type="checkbox" id="7"/>7
        <input type="checkbox" id="8"/>8
    </div>
</body>
</html>
<script>
    var synchronousFile = function(id){
        console.log("开始同步文件:" + id);
    }

    var proxySynchronousFile = (function(){
        var cache = [],
            timer;

            return function(id){
                cache.push(id);

                if(timer){
                    return;
                }
                timer = setTimeout(function(){
                    synchronousFile(cache.join(","));
                    clearTimeout(timer);
                    timer = null;
                    cache.length = 0;
                },2000);
            }
    })();



    var checkboxs = document.getElementsByTagName("input");
   for(var i =0;i<checkboxs.length;i++){
       checkboxs[i].onclick = function(){
           if(this.checked){
         
            proxySynchronousFile(this.id);
           }
       }
   }
</script>